﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Admin_provider_AddNewProviderWork : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            DataTable _dtCompany = General.GetComboItem("TH.ProviderCompany", false);
            ddlProviderCompanyName.DataSource = _dtCompany;
            ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompanyName.DataBind();

            ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));

            if (Page.Request.Params["WSId"] != null)
            {
                Int64 _workshopId = 0;
                if (Int64.TryParse(Page.Request.Params["WSId"].ToString(),out _workshopId))
                {
                    ShowWorkShopDetail(_workshopId);
                }
            }
        }
    }

    private void ShowWorkShopDetail(Int64 WorkShopId)
    {
        ClearForm();
        ProviderWorkShop _workshop = new ProviderWorkShop();
        ProviderWorkShopDB _workshopdb = new ProviderWorkShopDB();
        _workshop = _workshopdb.GetProviderWorkShop(WorkShopId);
        if (_workshop != null)
        {
            lblProviderWorkshopId.Text = WorkShopId.ToString();
            ddlProviderCompanyName.Text = _workshop.ProviderCompanyId.ToString();
            txtWorkShopName.Text = _workshop.WSName;
            txtBlockNo.Text = _workshop.BlockNo;
            txtAreaName.Text = _workshop.AreaName;
            txtBuildingName.Text = _workshop.BuildingName;
            txtLevelNo.Text = _workshop.LevelNo;
            txtRoomNo.Text = _workshop.RoomNo;
            txtStreetName.Text = _workshop.StreetName;
            txtPostCode.Text = _workshop.PostCode;
            ddlZone.Text = _workshop.ZoneName;
            txtCountry.Text = _workshop.Country;
            chkWorkShopStatus.Checked = _workshop.Status;
            btnSubmit.Text = "Update";
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null)
        {
            if (Page.IsValid)
            {
                ProviderWorkShop _workshop = new ProviderWorkShop();
                ProviderWorkShopDB _workshopdb = new ProviderWorkShopDB();
                if (lblProviderWorkshopId.Text.Length > 0)
                {
                    Int64 _workshopId = 0;
                    if (Int64.TryParse(lblProviderWorkshopId.Text, out _workshopId))
                    {
                        _workshop.WorkShopId = _workshopId;
                    }
                }
                _workshop.ProviderCompanyId = Convert.ToInt64(ddlProviderCompanyName.Text);
                _workshop.WSName = txtWorkShopName.Text;
                _workshop.BlockNo = txtBlockNo.Text;
                _workshop.AreaName = txtAreaName.Text;
                _workshop.BuildingName = txtBuildingName.Text;
                _workshop.LevelNo = txtLevelNo.Text;
                _workshop.RoomNo = txtRoomNo.Text;
                _workshop.StreetName = txtStreetName.Text;
                _workshop.PostCode = txtPostCode.Text;
                _workshop.ZoneName = ddlZone.Text;
                _workshop.Country = txtCountry.Text;
                _workshop.Status = chkWorkShopStatus.Checked;
                _workshop.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                _workshop.WorkShopId = _workshopdb.Insert(_workshop);

                if (_workshop.WorkShopId > 0)
                {
                    if (lblProviderWorkshopId.Text.Length > 0)
                    {
                        lblMessage.Text = "Update successfully!";
                    }
                    else
                    {
                        lblMessage.Text = "Insert successfully!";
                    }

                }
                else
                {
                    lblMessage.Text = "Inserting failed!";
                }
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }
    }
    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearForm();
    }

    private void ClearForm()
    {
        lblProviderWorkshopId.Text = "";
        ddlProviderCompanyName.Text = "-1";
        txtWorkShopName.Text = "";
        txtBlockNo.Text = "";
        txtAreaName.Text = "";
        txtBuildingName.Text = "";
        txtLevelNo.Text = "";
        txtRoomNo.Text = "";
        txtStreetName.Text = "";
        txtPostCode.Text = "";
        ddlZone.Text = "-1";
        txtCountry.Text = "";
        lblMessage.Text = "";
        btnSubmit.Text = "Submit";
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderWorkShop.aspx");
    }
}
